/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import controlador.exceptions.IllegalOrphanException;
import controlador.exceptions.NonexistentEntityException;
import entidad.Comanda;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidad.ComandaHasPlatillo;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author a
 */
public class ComandaJpaController implements Serializable {

    public ComandaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Comanda comanda) {
        if (comanda.getComandaHasPlatilloList() == null) {
            comanda.setComandaHasPlatilloList(new ArrayList<ComandaHasPlatillo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<ComandaHasPlatillo> attachedComandaHasPlatilloList = new ArrayList<ComandaHasPlatillo>();
            for (ComandaHasPlatillo comandaHasPlatilloListComandaHasPlatilloToAttach : comanda.getComandaHasPlatilloList()) {
                comandaHasPlatilloListComandaHasPlatilloToAttach = em.getReference(comandaHasPlatilloListComandaHasPlatilloToAttach.getClass(), comandaHasPlatilloListComandaHasPlatilloToAttach.getComandaHasPlatilloPK());
                attachedComandaHasPlatilloList.add(comandaHasPlatilloListComandaHasPlatilloToAttach);
            }
            comanda.setComandaHasPlatilloList(attachedComandaHasPlatilloList);
            em.persist(comanda);
            for (ComandaHasPlatillo comandaHasPlatilloListComandaHasPlatillo : comanda.getComandaHasPlatilloList()) {
                Comanda oldComandaOfComandaHasPlatilloListComandaHasPlatillo = comandaHasPlatilloListComandaHasPlatillo.getComanda();
                comandaHasPlatilloListComandaHasPlatillo.setComanda(comanda);
                comandaHasPlatilloListComandaHasPlatillo = em.merge(comandaHasPlatilloListComandaHasPlatillo);
                if (oldComandaOfComandaHasPlatilloListComandaHasPlatillo != null) {
                    oldComandaOfComandaHasPlatilloListComandaHasPlatillo.getComandaHasPlatilloList().remove(comandaHasPlatilloListComandaHasPlatillo);
                    oldComandaOfComandaHasPlatilloListComandaHasPlatillo = em.merge(oldComandaOfComandaHasPlatilloListComandaHasPlatillo);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Comanda comanda) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Comanda persistentComanda = em.find(Comanda.class, comanda.getIdComanda());
            List<ComandaHasPlatillo> comandaHasPlatilloListOld = persistentComanda.getComandaHasPlatilloList();
            List<ComandaHasPlatillo> comandaHasPlatilloListNew = comanda.getComandaHasPlatilloList();
            List<String> illegalOrphanMessages = null;
            for (ComandaHasPlatillo comandaHasPlatilloListOldComandaHasPlatillo : comandaHasPlatilloListOld) {
                if (!comandaHasPlatilloListNew.contains(comandaHasPlatilloListOldComandaHasPlatillo)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ComandaHasPlatillo " + comandaHasPlatilloListOldComandaHasPlatillo + " since its comanda field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<ComandaHasPlatillo> attachedComandaHasPlatilloListNew = new ArrayList<ComandaHasPlatillo>();
            for (ComandaHasPlatillo comandaHasPlatilloListNewComandaHasPlatilloToAttach : comandaHasPlatilloListNew) {
                comandaHasPlatilloListNewComandaHasPlatilloToAttach = em.getReference(comandaHasPlatilloListNewComandaHasPlatilloToAttach.getClass(), comandaHasPlatilloListNewComandaHasPlatilloToAttach.getComandaHasPlatilloPK());
                attachedComandaHasPlatilloListNew.add(comandaHasPlatilloListNewComandaHasPlatilloToAttach);
            }
            comandaHasPlatilloListNew = attachedComandaHasPlatilloListNew;
            comanda.setComandaHasPlatilloList(comandaHasPlatilloListNew);
            comanda = em.merge(comanda);
            for (ComandaHasPlatillo comandaHasPlatilloListNewComandaHasPlatillo : comandaHasPlatilloListNew) {
                if (!comandaHasPlatilloListOld.contains(comandaHasPlatilloListNewComandaHasPlatillo)) {
                    Comanda oldComandaOfComandaHasPlatilloListNewComandaHasPlatillo = comandaHasPlatilloListNewComandaHasPlatillo.getComanda();
                    comandaHasPlatilloListNewComandaHasPlatillo.setComanda(comanda);
                    comandaHasPlatilloListNewComandaHasPlatillo = em.merge(comandaHasPlatilloListNewComandaHasPlatillo);
                    if (oldComandaOfComandaHasPlatilloListNewComandaHasPlatillo != null && !oldComandaOfComandaHasPlatilloListNewComandaHasPlatillo.equals(comanda)) {
                        oldComandaOfComandaHasPlatilloListNewComandaHasPlatillo.getComandaHasPlatilloList().remove(comandaHasPlatilloListNewComandaHasPlatillo);
                        oldComandaOfComandaHasPlatilloListNewComandaHasPlatillo = em.merge(oldComandaOfComandaHasPlatilloListNewComandaHasPlatillo);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = comanda.getIdComanda();
                if (findComanda(id) == null) {
                    throw new NonexistentEntityException("The comanda with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Comanda comanda;
            try {
                comanda = em.getReference(Comanda.class, id);
                comanda.getIdComanda();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The comanda with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<ComandaHasPlatillo> comandaHasPlatilloListOrphanCheck = comanda.getComandaHasPlatilloList();
            for (ComandaHasPlatillo comandaHasPlatilloListOrphanCheckComandaHasPlatillo : comandaHasPlatilloListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Comanda (" + comanda + ") cannot be destroyed since the ComandaHasPlatillo " + comandaHasPlatilloListOrphanCheckComandaHasPlatillo + " in its comandaHasPlatilloList field has a non-nullable comanda field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(comanda);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Comanda> findComandaEntities() {
        return findComandaEntities(true, -1, -1);
    }

    public List<Comanda> findComandaEntities(int maxResults, int firstResult) {
        return findComandaEntities(false, maxResults, firstResult);
    }

    private List<Comanda> findComandaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Comanda.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Comanda findComanda(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Comanda.class, id);
        } finally {
            em.close();
        }
    }

    public int getComandaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Comanda> rt = cq.from(Comanda.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
